﻿using HP.UtilsLib;
using Logger;
using Mva.Recognition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;
using Windows.Storage;
using Windows.Storage.Streams;

namespace XiaoweiUWP.Settings
{
    public enum KeywordDetector{
        Microsoft,
        Tencent
    }

    public class TencentConfig
    {
        public bool LogEnabled { get; set; } = false;
        public int LogLevel { get; set; } = 3;
        public bool KwsEnabled { get; set; } = true;
    }

    public static class DebugConfig
    {
        private const int CheckVersionCycleDefault = 1000 * 60 * 60 * 24;
        private const string HostAddressDefault = "https://xiaowei.ext.hp.com";

        public delegate void EventHandler();
        public static event EventHandler Loaded;

        ///Debug Settings start
        public static bool SnDebugEnabled { get; private set; } = false;
        public static bool LogEnabled { get; private set; } = false;
        public static bool SaveKeywordEnabled { get; private set; } = false;
        public static bool SaveSpeechAfterMvaEnabled { get; private set; } = false;
        public static bool SaveSpeechEnabled { get; private set; } = false;
        public static bool SaveTTSEnabled { get; private set; } = false;
        public static bool QaAutoTestEnabled { get; private set; } = false;
        public static bool MetricsEnabled { get; private set; } = true;
        public static bool TestDriverList { get; private set; } = false;
        public static bool SupportDCMode { get; private set; } = false;

        public static String SSID = "";
        ///Debug Settings end
        public static KeywordDetector KeywordDetector { get; private set; } = KeywordDetector.Microsoft;
        
        public static int CheckVersionCycle { get; private set; } = CheckVersionCycleDefault;
        public static string HostAddress { get; private set; } = HostAddressDefault;

        public static MsSpeechRecognizerConfig MsSpeechConfig { get; private set; } = null;

        public static TencentConfig TencentConfig { get; private set; } = new TencentConfig();

        private static bool isLoaded = false;
        public static async Task<bool> LoadFromFileAsync(StorageFolder folder, string fileName)
        {
            Debug.WriteLine($"ReadDebugSettingsFromFile, isLoaded:{isLoaded}");

            if (isLoaded) return true;

            var txt = await FileHelper.ToStringAsync(folder, fileName).ConfigureAwait(false);
            if(string.IsNullOrEmpty(txt))
            {
                Debug.WriteLine("Debug File Not Found.");
                return false;
            }

            try
            {
                JsonObject jo = JsonObject.Parse(txt);

                //version check cycle
                DeserializeVersionCheckCycle(jo);

                //host
                DeserializeHost(jo);

                //keyword detector
                DeserializeKeywordDetector(jo);

                //debug settings
                DeserializeDebugConfig(jo);

                //MS speech config
                DeserializeMsConfig(jo);

                //Tencent config
                DeserializeTencentConfig(jo);

                //Metrics config
                DeserializeMetricsConfig(jo);

                //DriverList config
                DeserializeTestDriverList(jo);

                //SupportDCMode
                DeserializeSupportDCMode(jo);

                //SSID
                DeserializeSSID(jo);

                Debug.WriteLine($"SSID:{SSID}");
           
            }
            catch
            {
                Debug.WriteLine($"Format of {fileName} is not correct.");
                return false;
            }

            Debug.WriteLine($"ReadDebugSettingsFromFile, {txt}");
            isLoaded = true;
            Loaded?.Invoke();

            return true;
        }

        private static void DeserializeVersionCheckCycle(JsonObject jo)
        {
            CheckVersionCycle = (int)jo.GetNamedNumber("CheckVersionCycle", CheckVersionCycleDefault);
        }

        private static void DeserializeHost(JsonObject jo)
        {
            HostAddress = (string)jo.GetNamedString("HostAddress", HostAddressDefault);
        }
        private static void DeserializeKeywordDetector(JsonObject jo)
        {
            var detector = (string)jo.GetNamedString("KeywordDetector", KeywordDetector.Microsoft.ToString());
            switch (detector.ToLower())
            {
                case "microsoft":
                    KeywordDetector = KeywordDetector.Microsoft;
                    break;
                case "tencent":
                    KeywordDetector = KeywordDetector.Tencent;
                    break;
                default: break;
            }
        }
        private static void DeserializeDebugConfig(JsonObject jo)
        {
            var subobject = jo.GetNamedObject("Debug", null);
            if (subobject != null)
            {
                SnDebugEnabled = subobject.GetNamedBoolean("SnDebugEnabled", false);
                LogEnabled = subobject.GetNamedBoolean("LogEnabled", false);
                SaveKeywordEnabled = subobject.GetNamedBoolean("SaveKeywordEnabled", false);
                SaveSpeechAfterMvaEnabled = subobject.GetNamedBoolean("SaveSpeechAfterMvaEnabled", false);
                SaveSpeechEnabled = subobject.GetNamedBoolean("SaveSpeechEnabled", false);
                SaveTTSEnabled = subobject.GetNamedBoolean("SaveTTSEnabled", false);
                QaAutoTestEnabled = subobject.GetNamedBoolean("QaAutoTestEnabled", false);
            }
        }

        private static void DeserializeMsConfig(JsonObject jo)
        {
            var subobject = jo.GetNamedObject("MsConfig", null);
            if (subobject != null)
            {
                MsSpeechConfig = new MsSpeechRecognizerConfig()
                {
                    SubscriptionKey = subobject.GetNamedString("SubscriptionKey", ""),
                    AzureRegion = subobject.GetNamedString("AzureRegion", ""),
                    CustomSpeechId = subobject.GetNamedString("CustomSpeechId", ""),
                };
            }
        }

        private static void DeserializeTencentConfig(JsonObject jo)
        {
            var subobject = jo.GetNamedObject("TencentConfig", null);
            if (subobject != null)
            {
                TencentConfig = new TencentConfig()
                {
                    LogEnabled = subobject.GetNamedBoolean("LogEnabled", false),
                    LogLevel = (int)subobject.GetNamedNumber("LogLevel", 3),
                    KwsEnabled = subobject.GetNamedBoolean("KwsEnabled", true),
                };
            }
        }

        private static void DeserializeMetricsConfig(JsonObject jo)
        {
            MetricsEnabled = jo.GetNamedBoolean("MetricsEnabled", true);
        }

        private static void DeserializeTestDriverList(JsonObject jo)
        {
            TestDriverList = jo.GetNamedBoolean("TestDriverList", false);
        }

        private static void DeserializeSupportDCMode(JsonObject jo)
        {
            SupportDCMode = jo.GetNamedBoolean("SupportDCMode", false);
        }

        private static void DeserializeSSID(JsonObject jo)
        {
            SSID = jo.GetNamedString("SSID", "");
        }
    }
}
